package com.avecsys.quotemanager.ui.components;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;

import org.icefaces.ace.component.fileentry.FileEntry;
import org.icefaces.ace.component.fileentry.FileEntryEvent;
import org.icefaces.ace.component.fileentry.FileEntryResults;
import org.icefaces.ace.component.fileentry.FileEntryResults.FileInfo;

import com.avecsys.quotemanager.constants.ApplicationConstants;
import com.icesoft.faces.component.ext.HtmlInputTextarea;
import com.icesoft.faces.component.ext.HtmlSelectBooleanCheckbox;
import com.icesoft.faces.context.effects.JavascriptContext;

/**
 * @author Sam Sundar K
 */
public class QuoteModifyDialog implements Serializable
{
	private String dialogId;

	private String requestJSON;

	private boolean modifyExistingQuoteAttachment = false;

	private boolean quoteAttachmentRequired = false;

	private boolean quoteAttachmentDisabled = true;

	private HtmlSelectBooleanCheckbox widgetModifyExistingQuoteAttachment = new HtmlSelectBooleanCheckbox();

	private HtmlInputTextarea widgetComments = new HtmlInputTextarea();

	private FileEntry widgetQuoteAttachment = new FileEntry();

	private String comments;

	private Double newQuoteValue;

	public QuoteModifyDialog(String dialogId)
	{
		this.dialogId = dialogId;
	}

	public void actionModifyExistingQuoteAttachment(ValueChangeEvent valueChangeEvent)
	{
		setModifyExistingQuoteAttachment((Boolean) valueChangeEvent.getNewValue());
		setQuoteAttachmentRequired((Boolean) valueChangeEvent.getNewValue());
		setQuoteAttachmentDisabled(!(Boolean) valueChangeEvent.getNewValue());
		getWidgetComments().setRequired(true);
		getWidgetComments().validate(FacesContext.getCurrentInstance());
	}

	public void actionQuoteAttachmentListener(FileEntryEvent fileEntryEvent)
	{
		FileEntry fileEntry = (FileEntry) fileEntryEvent.getComponent();
		FileEntryResults fileEntryResults = fileEntry.getResults();
		List<FileInfo> fileInfoList = fileEntryResults.getFiles();
		for (FileInfo fileInfo : fileInfoList)
		{
			if (fileInfo.isSaved())
			{
				FacesContext facesContext = FacesContext.getCurrentInstance();
				facesContext.getExternalContext()
						.getSessionMap()
						.put(ApplicationConstants.QUOTE_TRANSACTION_ATTACHEMNT_FILE_INFO, fileInfo);
			}
			else
			{

			}
		}
	}

	/**
	 * @return the comments
	 */
	public String getComments()
	{
		return comments;
	}

	/**
	 * @return the dialogId
	 */
	public String getDialogId()
	{
		return dialogId;
	}

	/**
	 * @return the newQuoteValue
	 */
	public Double getNewQuoteValue()
	{
		return newQuoteValue;
	}

	/**
	 * @return the requestJSON
	 */
	public String getRequestJSON()
	{
		return requestJSON;
	}

	/**
	 * @return the widgetComments
	 */
	public HtmlInputTextarea getWidgetComments()
	{
		return widgetComments;
	}

	/**
	 * @return the widgetModifyExistingQuoteAttachment
	 */
	public HtmlSelectBooleanCheckbox getWidgetModifyExistingQuoteAttachment()
	{
		return widgetModifyExistingQuoteAttachment;
	}

	/**
	 * @return the widgetQuoteAttachment
	 */
	public FileEntry getWidgetQuoteAttachment()
	{
		return widgetQuoteAttachment;
	}

	public void hideDialog()
	{
		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), getDialogId()
				+ ".hide();");
	}

	public void hideDialog(ActionEvent actionEvent)
	{
		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), getDialogId()
				+ ".hide();");
	}

	/**
	 * @return the modifyExistingQuoteAttachment
	 */
	public boolean isModifyExistingQuoteAttachment()
	{
		return modifyExistingQuoteAttachment;
	}

	/**
	 * @return the quoteAttachmentDisabled
	 */
	public boolean isQuoteAttachmentDisabled()
	{
		return quoteAttachmentDisabled;
	}

	/**
	 * @return the quoteAttachmentRequired
	 */
	public boolean isQuoteAttachmentRequired()
	{
		return quoteAttachmentRequired;
	}

	public void resetFields()
	{
		getWidgetModifyExistingQuoteAttachment().setSelected(false);
		getWidgetComments().setRequired(true);
		getWidgetComments().setSubmittedValue("");
		FileEntry widgetFileEntry = (FileEntry) getWidgetComments().findComponent("fileAttachmentId");
		if (widgetFileEntry != null)
		{
			widgetFileEntry.setDisabled(true);
		}
		getWidgetQuoteAttachment().setDisabled(true);
		getWidgetQuoteAttachment().setRequired(false);
	}

	/**
	 * @param comments the comments to set
	 */
	public void setComments(String comments)
	{
		this.comments = comments;
	}

	/**
	 * @param dialogId the dialogId to set
	 */
	public void setDialogId(String dialogId)
	{
		this.dialogId = dialogId;
	}

	/**
	 * @param modifyExistingQuoteAttachment the modifyExistingQuoteAttachment to set
	 */
	public void setModifyExistingQuoteAttachment(boolean modifyExistingQuoteAttachment)
	{
		this.modifyExistingQuoteAttachment = modifyExistingQuoteAttachment;
	}

	/**
	 * @param newQuoteValue the newQuoteValue to set
	 */
	public void setNewQuoteValue(Double newQuoteValue)
	{
		this.newQuoteValue = newQuoteValue;
	}

	/**
	 * @param quoteAttachmentDisabled the quoteAttachmentDisabled to set
	 */
	public void setQuoteAttachmentDisabled(boolean quoteAttachmentDisabled)
	{
		this.quoteAttachmentDisabled = quoteAttachmentDisabled;
	}

	/**
	 * @param quoteAttachmentRequired the quoteAttachmentRequired to set
	 */
	public void setQuoteAttachmentRequired(boolean quoteAttachmentRequired)
	{
		this.quoteAttachmentRequired = quoteAttachmentRequired;
	}

	/**
	 * @param requestJSON the requestJSON to set
	 */
	public void setRequestJSON(String requestJSON)
	{
		this.requestJSON = requestJSON;
	}

	/**
	 * @param widgetComments the widgetComments to set
	 */
	public void setWidgetComments(HtmlInputTextarea widgetComments)
	{
		this.widgetComments = widgetComments;
	}

	/**
	 * @param widgetModifyExistingQuoteAttachment the widgetModifyExistingQuoteAttachment to set
	 */
	public void setWidgetModifyExistingQuoteAttachment(
			HtmlSelectBooleanCheckbox widgetModifyExistingQuoteAttachment)
	{
		this.widgetModifyExistingQuoteAttachment = widgetModifyExistingQuoteAttachment;
	}

	/**
	 * @param widgetQuoteAttachment the widgetQuoteAttachment to set
	 */
	public void setWidgetQuoteAttachment(FileEntry widgetQuoteAttachment)
	{
		this.widgetQuoteAttachment = widgetQuoteAttachment;
	}

	public void showDialog()
	{
		resetFields();
		JavascriptContext.addJavascriptCall(FacesContext.getCurrentInstance(), getDialogId()
				+ ".show();");
	}
}
